package com.drojian.root;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.drojian.demo.R;
import com.drojian.root.database.SqlUser;


public class RegisterFragment extends Fragment implements View.OnClickListener {
    private View view;
    private EditText userName,passWord,enterPassWord;
    private Button register;
    private boolean isPasswordVisible = false; // 用于追踪密码的显示状态

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.root_fragment_register, container, false);
        initView();


        return view;
    }

    private void initView() {
         userName = view.findViewById(R.id.userName);
         passWord = view.findViewById(R.id.passWord);
         enterPassWord = view.findViewById(R.id.enterPassWord);
         register = view.findViewById(R.id.register);
         register.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register:
                String name = userName.getText().toString();
                String pass1 = passWord.getText().toString();
                String pass2 = enterPassWord.getText().toString();
                SqlUser user = new SqlUser(getContext(), "user.db", null, 1);
                SQLiteDatabase db = user.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("username", name);
                values.put("password",pass1);
                if(pass1.equals(pass2)){
                    db.insert("user",null,values);
                    values.clear();
                    Toast.makeText(getContext(),"注册成功",Toast.LENGTH_SHORT).show();
                    FragmentManager manager = getActivity().getSupportFragmentManager();
                    Fragment loginFragment = manager.findFragmentById(R.id.root_fragment);
                    FragmentTransaction transaction = manager.beginTransaction();
                    transaction.show(loginFragment);
                    transaction.remove(this);
                    transaction.commit();
                }else {
                    Toast.makeText(getContext(),"两次密码不一致",Toast.LENGTH_SHORT).show();
                }

        }
    }
    public void enter(View view) {
        isPasswordVisible = !isPasswordVisible; // 切换显示状态
        if (isPasswordVisible) {
            // 设为可见
            passWord.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
           // enterPassWord.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            // 设为不可见
            passWord.setTransformationMethod(PasswordTransformationMethod.getInstance());
            //enterPassWord.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }

        // 更新EditText以应用新的转换方法
        //pass.postInvalidate();
    }
}